package com.unlcn.ils.kas.dao;

import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.spring.boot.starter.MybatisPlusAutoConfiguration;
import com.unlcn.ils.kas.dao.infrastructure.Repository.MabatisGeelyOnWayRepository;
import com.unlcn.ils.kas.dao.model.onway.GeelyOnWay;
import com.unlcn.ils.kas.dao.model.onway.GeelyOnWayRepository;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

/*
 * @Description: Mabatis-Plus最小化测试Bean上下文尝试
 * 方法一:
 * @..Test注解 配合 @ImportAutoConfiguration 及 @ActiveProfiles 注解 构建基本上下文
 * 再配合 @Import注解引入测试class
 * 注意用 @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) 控制@JdbcTest的默认数据源替换行为
 * 
 * @link https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-tests
 * @Author: zharui
 * */
@RunWith(SpringRunner.class)
@JdbcTest
@ImportAutoConfiguration(MybatisPlusAutoConfiguration.class)
@Import({ MabatisGeelyOnWayRepository.class })
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("test")
public class GeelyOnWayRepositoryTestV1 {

  @Configuration
  public static class TestInit {

  }

  @Autowired
  GeelyOnWayRepository onwayRepository;

  @Test
  public void shouldGetInfos() {
    Date now = new Date();
    Calendar cald = Calendar.getInstance();
    cald.set(2019, 6, 22);
    Date last_call_time = cald.getTime();
    List<GeelyOnWay> infos = onwayRepository.selectByLocationZone(last_call_time, now);
    System.out.println(infos.size());
    assertTrue(!infos.isEmpty());
  }

  @Test
  public void shouldGetNopushed() {
    Calendar cald = Calendar.getInstance();
    cald.set(2019, 6, 22);
    Date last_call_time = cald.getTime();
    List<GeelyOnWay> nopushed = onwayRepository.selectUnpushed(last_call_time);
    System.out.println(nopushed.size());
    assertTrue(!nopushed.isEmpty());
    System.out.println(nopushed.get(0));
  }

  @Test
  public void shouldCountUnpushed() {
    ArrayList<String> vinList = new ArrayList<String>();
    vinList.add("L6T7804Z3KW086938");
    vinList.add("L6T7804Z8KW087504");
    vinList.add("L6T7804Z8KW086966");
    long ct = onwayRepository.selectUnpushedCtByVINList(vinList);
    System.out.println("shouldCountUnpushed .. " + ct);
    assertTrue(ct > 0);
  }

  @Test
  public void shouldConfirmPushed() {
    ArrayList<String> vinList = new ArrayList<String>();
    vinList.add("L6T7804Z3KW086938");
    vinList.add("L6T7804Z8KW087504");
    vinList.add("L6T7804Z8KW086966");
    Date now = new Date();
    onwayRepository.confirmPushedBatch(vinList, now);

    // Wrapper<GeelyOnWay> wrapper = new Wrapper<GeelyOnWay>() {

    // private static final long serialVersionUID = 1L;

    // @Override
    // public String getSqlSegment() {
    // return "where vcvin in
    // ('L6T7804Z3KW086938','L6T7804Z8KW087504','L6T7804Z8KW086966') and bpushed=0";
    // }
    // };

    long selectCount = onwayRepository.selectUnpushedCtByVINList(vinList);
    assertTrue(0 == selectCount);
  }
}
